package cgl.hadoopsensorgrid.sensorgrid.util;

import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.io.RawComparator;

/***
 * 
 * Comparator for comparing two serializedText instances.
 * 
 * @author Chao Sun
 * 
 */
public class serializedTextComparator implements RawComparator<serializedText> {

	/**
	 * Override.
	 */
	@Override
	public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
		return WritableComparator.compareBytes(b1, s1, l1, b2, s2, l2);
	}

	/**
	 * Override.
	 */
	@Override
	public int compare(serializedText o1, serializedText o2) {
		int PID1 = o1.getPID();
		int PID2 = o2.getPID();
		/*if (PID1 < PID2) {
			return -1;
		} else if (PID1 > PID2) {
			return 1;
		}*/
		
		if (PID1 != PID2) {
			System.out.println("Comparator error");
			System.exit(0);
		}
		
		int ID1 = o1.getID();
		int ID2 = o2.getID();
		return ID1 == ID2 ? 0 : (ID1 < ID2 ? -1 : 1);
	}

}